// URL 类型常量
export const URL_TYPE = {
    ACP: 'acp',
    DATA_SERVICES: 'dataservices',
    UNKNOWN: 'unknown'
};

// URL 标识
const URL_FLAGS = {
    ACP: '/console-acp/',
    DATA_SERVICES: '/console-dataservices/'
};

// URL 模式匹配正则
const URL_PATTERNS = {
    ACP: /\/console-acp\/workspace\/(.*?)~(.*?)~(.*?)\//,
    DATA_SERVICES_PARAM: /\/console-dataservices\/project\/(.*?)\/(.*?)\?workspace=(.*?)~(.*?)~(.*?)(?:$|&)/,
    DATA_SERVICES_VIEW: /\/console-dataservices\/project\/(.*?)\/(.*?)\/view\/(.*?)~(.*?)~(.*?)~(.*)/
};

/**
 * 获取URL类型
 * @param {string} url - 需要检查的URL
 * @returns {string} URL类型
 */
export function getUrlType(url) {
    if (url.includes(URL_FLAGS.ACP)) return URL_TYPE.ACP;
    if (url.includes(URL_FLAGS.DATA_SERVICES)) return URL_TYPE.DATA_SERVICES;
    return URL_TYPE.UNKNOWN;
}

/**
 * 生成新的URL
 * @param {string} currentUrl - 当前URL
 * @param {string} selectedCluster - 选中的集群
 * @param {string} selectedNamespace - 选中的命名空间
 * @returns {{success: boolean, url?: string, error?: string}} 处理结果
 */
export function generateNewUrl(currentUrl, selectedCluster, selectedNamespace) {
    const urlType = getUrlType(currentUrl);
    
    switch (urlType) {
        case URL_TYPE.ACP:
            return handleAcpUrl(currentUrl, selectedCluster, selectedNamespace);
        case URL_TYPE.DATA_SERVICES:
            return handleDataServicesUrl(currentUrl, selectedCluster, selectedNamespace);
        default:
            return {
                success: false,
                error: "不支持的页面类型"
            };
    }
}

/**
 * 处理 ACP URL
 * @private
 */
function handleAcpUrl(currentUrl, selectedCluster, selectedNamespace) {
    const match = currentUrl.match(URL_PATTERNS.ACP);
    if (!match) {
        return {
            success: false,
            error: "无法识别的ACP URL格式"
        };
    }

    const [, project, currentCluster, currentNamespace] = match;
    // 如果都选择使用当前值，提示用户
    if (!selectedCluster && !selectedNamespace) {
        return {
            success: false,
            error: "未选择任何更改，将留在当前页面"
        };
    }

    const newUrl = currentUrl.replace(
        URL_PATTERNS.ACP,
        `/console-acp/workspace/${project}~${selectedCluster || currentCluster}~${selectedNamespace || currentNamespace}/`
    );

    return {
        success: true,
        url: newUrl
    };
}

/**
 * 处理 DataServices URL
 * @private
 */
function handleDataServicesUrl(currentUrl, selectedCluster, selectedNamespace) {
    // 尝试匹配参数模式
    let match = currentUrl.match(URL_PATTERNS.DATA_SERVICES_PARAM);
    if (match) {
        const [fullMatch, p1, p2, project, currentCluster, currentNamespace] = match;
        
        // 如果都选择使用当前值，提示用户
        if (!selectedCluster && !selectedNamespace) {
            return {
                success: false,
                error: "未选择任何更改，将留在当前页面"
            };
        }
        const baseUrl = currentUrl.substring(0, currentUrl.indexOf('?'));
        const queryStart = currentUrl.indexOf('?');
        const queryString = currentUrl.substring(queryStart);
        
        const newQueryString = queryString.replace(
            /workspace=.*?(?:$|&)/,
            `workspace=${project}~${selectedCluster || currentCluster}~${selectedNamespace || currentNamespace}${fullMatch.endsWith('&') ? '&' : ''}`
        );

        return {
            success: true,
            url: baseUrl + newQueryString
        };
    }

    // 尝试匹配 view 模式
    match = currentUrl.match(URL_PATTERNS.DATA_SERVICES_VIEW);
    if (match) {
        const [fullMatch, p1, p2, project, currentCluster, currentNamespace, p6] = match;
        
        // 如果都选择使用当前值，提示用户
        if (!selectedCluster && !selectedNamespace) {
            return {
                success: false,
                error: "未选择任何更改，将留在当前页面"
            };
        }

        // 获取 URL 的基础部分（包括域名等）
        const baseUrl = currentUrl.substring(0, currentUrl.indexOf('/console-dataservices'));
        const newPath = `/console-dataservices/project/${p1}/${p2}/view/${project}~${selectedCluster || currentCluster}~${selectedNamespace || currentNamespace}~${p6}`;

        return {
            success: true,
            url: baseUrl + newPath
        };
    }

    return {
        success: false,
        error: "无法识别的DataServices URL格式"
    };
} 